#include "NetContext.hpp"

namespace TWT {
namespace NetWork {

NetContext::NetContext(std::size_t pool_size)
{
      if (pool_size == 0)
      {
          pool_size = 2;
      }

    for(auto i = pool_size - 1; i > 0; --i)
    {
        threads_.emplace_back([this]
                {
                    ioContext_.run();
                });
    }

    work_.push_back(net::require(ioContext_.get_executor(),net::execution::outstanding_work.tracked));

}

NetContext::~NetContext()
{

    stop();
    for(auto& thread : threads_)
    {
        thread.join();
    }
}


void NetContext::stop()
{
    ioContext_.stop();
}

net::io_context& NetContext::getIoContext()
{
    return ioContext_;
}


} // NetWork
} // TWT
